﻿using System;





namespace ScrumFactory.Services.Web {

    public partial class MemberImage : System.Web.UI.Page {

        private ITeamService teamService = null;
        private ITeamService TeamService {
            get {
                if (teamService != null)
                    return teamService;                
                teamService = ScrumFactory.Services.Web.Application.CompositionContainer.GetExportedValue<ITeamService>();
                return teamService;
            }
            
        }

        private byte[] GetDefaultImage() {
            System.IO.FileStream fs = System.IO.File.OpenRead(Server.MapPath("/App_data/" + "none.png"));
            byte[] image = new byte[fs.Length];
            fs.Read(image, 0, image.Length);
            fs.Close();
            return image;
        }

        protected void Page_Load(object sender, EventArgs e) {

            byte[] image = null;
            string memberUId = Request["memberUId"];
            if (String.IsNullOrEmpty(memberUId))
                image = GetDefaultImage();

            
            MemberAvatar avatar = TeamService.GetMemberAvatar(memberUId);
            if (avatar == null) {
                image = GetDefaultImage();
            } else
                image = avatar.AvatarImage;

            Response.ContentType = "image/png";
            Response.BinaryWrite(image);

        }
    }
}